package com.ipt.frm.pw.filter;


import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ipt.frm.pw.Mensaje;
import com.ipt.frm.pw.TipoMensaje;

public class FilterCheckUser extends FiltroMaestro{
	static final private String PATH_PROPERTIES_MSG = "/WEB-INF/config/messages.properties";
	ServletContext context;

	 @Override
	  public void doFilter(final ServletRequest request,final ServletResponse response,
		                       FilterChain chain)
		       throws java.io.IOException, javax.servlet.ServletException  { 
		 HttpServletRequest req = (HttpServletRequest) request;
	     HttpServletResponse resp = (HttpServletResponse) response;
	     String url = req.getServletPath();
	     boolean allowedRequest = false;
		 
		 try{
			 
			 FileInputStream in = new FileInputStream(PATH_PROPERTIES_MSG);  
			 propertiesMsg.load(in);
	    		/*propertiesMsg.load(context.getResourceAsStream(PATH_PROPERTIES_MSG));*/

	    	}catch(IOException e){
	    		e.printStackTrace();
	    	}
		 
		chain.doFilter(request, response);
		
        if(urlList.contains(url)) {
            allowedRequest = true;
        }
        
        if (!allowedRequest) {
            HttpSession session = req.getSession();
    		if(session.getAttribute("username") == null){
    			Mensaje mensaje;
    			mensaje = new Mensaje(TipoMensaje.INFO,propertiesMsg.getProperty("includes.checkUser"));
    			//forward
    			request.setAttribute("MSG", mensaje);
    			request.getRequestDispatcher("login.jsp").forward(request, response);
    		}
        }
        
		

		 
	  }
}
